package com.example.mytest.utils;


import com.blankj.utilcode.constant.TimeConstants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class TimeUtils {

    public static final String DATE_PATTERN = "MM-dd HH:mm";
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    public final static long day = 24 * hour;// 1天
    public static final long YEAR = day * 365;
    public static final long MONTH = day * 31;
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年
    private static final long MINUTE_SECONDS = 60; //1分钟多少秒
    private static final long HOUR_SECONDS = MINUTE_SECONDS * 60;
    private static final long DAY_SECONDS = HOUR_SECONDS * 24;
    private static final long YEAR_SECONDS = DAY_SECONDS * 365;
    public static String FORMAT_SHORT_MONTH = "yyyy年MM月";
    /**
     * 英文全称 如：2010-12-01 23:15:06
     */
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
    /**
     * 英文全称 如12-01
     */
    public static String FORMAT_SHORT_DAY = "MM-dd";

    /**
     * 英文全称 如：23:15
     */
    public static String FORMAT_SHORT_TIME = "HH:mm";
    /**
     * 英文全称 如：23:15
     */
    public static String FORMAT_LONG_TIME = "HH:mm:ss";

    /**
     * 英文全称 如：2010-12-01 23:15
     */
    public static String FORMAT_FOR_CBC = "yyyy-MM-dd HH:mm";
    /**
     * 英文全称 如：2010/12/01 23:15
     */
    public static String FORMAT_CBC = "yyyy/MM/dd HH:mm";
    /**
     * 中文简写 如：2010年12月01日
     */
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd日";
    /**
     * 英文简写（默认）如：2010-12-01
     */
    public static String FORMAT_SHORT = "yyyy-MM-dd";
    /**
     * 英文简写（默认）如：2010.12.01
     */
    public static String FORMAT_SHORT_ = "yyyy.MM.dd";


    /**
     * 根据秒数获取时间串
     *
     * @param second (eg: 100s)
     * @return (eg : 00 : 01 : 40)
     */
    public static String getTimeStrBySecond(int second) {
        if (second <= 0) {

            return "00:00";
        }

        StringBuilder sb = new StringBuilder();
        int hours = second / 3600;
        if (hours > 0) {

            second -= hours * 3600;
        }

        int minutes = second / 60;
        if (minutes > 0) {

            second -= minutes * 60;
        }
        return ((minutes >= 10 ? (minutes + "") : ("0" + minutes)) + ":"
                + (second >= 10 ? (second + "") : ("0" + second)));
//        return (hours > 10 ? (hours + "")
//                : ("0" + hours) + ":" + (minutes > 10 ? (minutes + "") : ("0" + minutes)) + ":"
//                + (second > 10 ? (second + "") : ("0" + second)));
    }


    /**
     * 格式化时间
     *
     * @param timestamp
     * @return
     */
    public static String toFormatTime(long timestamp) {
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
        return sdf.format(date);
    }

    /**
     * 时间戳转化为时间格式
     *
     * @param timestampSec 毫秒
     * @param pattern
     * @return
     */
    public static String toFormatTime(long timestampSec, String pattern) {
        Date date = new Date(timestampSec);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }


    /**
     * @param sourceSeconds 时间戳 秒
     * @return
     */
    public static String 语义化街区相关时间差(int sourceSeconds) {
        SimpleDateFormat hh_mm;
        int 日历日期差 = 计算距现在的时间差(sourceSeconds, 1);
        int 小时差 = 计算距现在的时间差(sourceSeconds, 2);
        int 分钟差 = 计算距现在的时间差(sourceSeconds, 3);
        if (日历日期差 == Integer.MIN_VALUE || 小时差 == Integer.MIN_VALUE || 分钟差 == Integer.MIN_VALUE) {
            return "";
        }
        if (日历日期差 < 0) { //跨天（历史时间）
            if (小时差 < 0) {
                if (Math.abs(小时差) < 24) {
                    hh_mm = new SimpleDateFormat("昨天 HH:mm", Locale.CHINA);
                } else {
                    hh_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                }
            } else {
                hh_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            }
        } else if (日历日期差 == 0) { //同一天（当天）(日历天)
            if (小时差 < 0) {
                hh_mm = new SimpleDateFormat("HH:mm");
            } else if (小时差 == 0) {
                if (分钟差 < 0) {
                    return Math.abs(分钟差) + "分钟前";
                } else if (分钟差 == 0) {
                    return "刚刚";
                } else {
                    hh_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                }
            } else {
                hh_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            }
        } else { //跨天（未来的时间）
            hh_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
        return hh_mm.format(sourceSeconds * 1000l);
    }


    public static int 计算距现在的时间差(int sourceSeconds, int type) {
        long sourceMS = sourceSeconds * 1000l;
        int retVal;
        switch (type) {
            case 1: //日历日期差

                retVal = diffDay(sourceMS, System.currentTimeMillis());
                break;
            case 2://小时差
                retVal = (int) com.blankj.utilcode.util.TimeUtils.getTimeSpanByNow(sourceMS, TimeConstants.HOUR);
                break;
            case 3://分钟差
                retVal = (int) com.blankj.utilcode.util.TimeUtils.getTimeSpanByNow(sourceMS, TimeConstants.MIN);
                break;
            default:
                retVal = Integer.MIN_VALUE;
                break;
        }
        return retVal;
    }


    public static String calcPurchaseStausDayStr(long 采购单截止时间戳秒, String status) {
        int leftentrydays = calcPurchaseStausDay(采购单截止时间戳秒);
        return leftentrydaysToStr(leftentrydays, status);
    }

    /**
     * 根据剩余天数和采购单状态计算采购单
     *
     * @param leftentrydays 采购单剩余天数
     * @param status        采购单状态
     * @return
     */
    public static String leftentrydaysToStr(int leftentrydays, String status) {
        if (status.equals("3")) { //已经过期的就不去计算剩余天数了，已过期状态以status为准
            return "已过期";
        } else {
            if (leftentrydays > 0) {
                return leftentrydays + "天";
            } else if (leftentrydays < 0) {
                return "已过期";
            } else {
                return "1天";
            }
        }
    }

    /**
     * 计算采购单剩余天数
     * 小于零为过期，等于0为不满24小时（显示一天）
     * 仅用于展示，不要用此方法返回值判断采购单状态
     *
     * @param 采购单截止时间戳秒
     * @return
     */
    public static int calcPurchaseStausDay(long 采购单截止时间戳秒) {
        long nowMilliseconds = System.currentTimeMillis() / 1000;
        long passed = (采购单截止时间戳秒 - nowMilliseconds);//转为秒

        if (passed > DAY_SECONDS) {//剩余天数
            return (int) ((passed / DAY_SECONDS) + 1);
        } else if (passed < 0) { //过期天数
            return (int) (passed / DAY_SECONDS);
        } else { //不足一天
            return 1;
        }
    }

    /**
     * 计算日期差（根据实际的日历天）
     * 例如  2019年12月31日23点59分59秒  和 2020年1月1日 12点00分00秒  差距为  1天
     *
     * @param startMs 开始的时间戳
     * @param endMs   结束的时间戳
     * @return 返回值为正数 代表开始时间比结束时间早
     * 返回值为负数 代表开始时间比结束时间晚
     * 返回值为0 ，代表开始时间和结束时间在同一天 h或者程序出现了异常
     * @throws ParseException
     */
    public static int diffDay(long startMs, long endMs) {
        try {
            int diffDay = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            Date dBegin = sdf.parse(sdf.format(startMs));
            calBegin.setTime(dBegin);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            Date dEnd = null;
            dEnd = sdf.parse(sdf.format(endMs));

            calEnd.setTime(dEnd);
            // 测试此日期是否在指定日期之后
            if (dEnd.after(calBegin.getTime())) {
                while (dEnd.after(calBegin.getTime())) {
                    // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                    calBegin.add(Calendar.DAY_OF_MONTH, 1);
                    diffDay++;
                }
            } else {
                while (dEnd.before(calBegin.getTime())) {
                    // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                    calBegin.add(Calendar.DAY_OF_MONTH, -1);
                    diffDay++;
                }
            }
            if (diffDay == 0) {
                return diffDay;
            } else {
                if (dBegin.before(dEnd)) {
                    return diffDay;
                } else {
                    return -diffDay;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }


    /**
     * 使用用户格式提取字符串日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */
    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            return df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 返回时间戳
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */
    public static String parseToData(String strDate, String pattern) {
        long val = parseToLongData(strDate, pattern);
        return val == 0 ? "" : val + "";
    }

    public static String formatString(String strDate, String pattern) {
        long val = parseToLongData(strDate, "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(new Date(val));
    }

    /**
     * 返回时间戳
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */
    public static long parseToLongData(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            df.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            long time = (df.parse(strDate).getTime()) / 1000;
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /*
     * 返回当前的时间年月日
     *
     * */
    public static String showYearMothDay(long oldMilliseconds) {
        return toFormatTime(oldMilliseconds * 1000, FORMAT_SHORT);
    }

    /*
     * 返回当前的时间年月日
     *
     * */
    public static String showYearMothDayChines(long oldMilliseconds) {
        return toFormatTime(oldMilliseconds, FORMAT_SHORT_CN);
    }

    /*
     * 返回当前的时间年月
     *
     * */
    public static String showYearMoth(long oldMilliseconds) {
        return toFormatTime(oldMilliseconds * 1000, FORMAT_SHORT_MONTH);
    }

    /**
     * 开始日期范围设定
     * 已年为单位计量范围
     *
     * @param startYear
     * @param endYear
     * @return
     */
    public static Integer[] getYearChange(int startYear, int endYear) {
        try {
            //时间集合
            Integer[] yearMonthDay = new Integer[9];
            Calendar calendar = Calendar.getInstance();
            //开始
            Calendar calendarStart = Calendar.getInstance();
            long startTimes = calendar.getTimeInMillis() - (startYear * YEAR);
            calendarStart.setTimeInMillis(startTimes);
            // 获取年
            int yearStart = calendarStart.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthStart = calendarStart.get(Calendar.MONTH) + 1;
            // 获取日
            int dayStart = calendarStart.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[0] = yearStart;
            yearMonthDay[1] = monthStart;
            yearMonthDay[2] = dayStart;
            //当前
            // 获取年
            int year = calendar.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int month = calendar.get(Calendar.MONTH) + 1;
            // 获取日
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[3] = year;
            yearMonthDay[4] = month;
            yearMonthDay[5] = day;
            //结束
            Calendar calendarEnd = Calendar.getInstance();
            long endTimes = calendar.getTimeInMillis() + (endYear * YEAR);
            calendarEnd.setTimeInMillis(endTimes);
            // 获取年
            int yearEnd = calendarEnd.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthEnd = calendarEnd.get(Calendar.MONTH) + 1;
            // 获取日
            int dayEnd = calendarEnd.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[6] = yearEnd;
            yearMonthDay[7] = monthEnd;
            yearMonthDay[8] = dayEnd;
            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 结束日期范围设定，已开始时间为基准
     * 已年为单位计量范围
     *
     * @param startYear
     * @param startMonth
     * @param startDay
     * @param endYear
     * @return
     */
    public static Integer[] getYearChangeEnd(int startYear, int startMonth, int startDay, int endYear) {
        try {
            //时间集合
            Integer[] yearMonthDay = new Integer[9];
            Calendar calendar = Calendar.getInstance();
            //开始
            yearMonthDay[0] = startYear;
            yearMonthDay[1] = startMonth;
            yearMonthDay[2] = startDay;
            //当前
            yearMonthDay[3] = startYear;
            yearMonthDay[4] = startMonth;
            yearMonthDay[5] = startDay;
            //结束
            Calendar calendarEnd = Calendar.getInstance();
            long endTimes = calendar.getTimeInMillis() + (endYear * YEAR);
            calendarEnd.setTimeInMillis(endTimes);
            // 获取年
            int yearEnd = calendarEnd.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthEnd = calendarEnd.get(Calendar.MONTH) + 1;
            // 获取日
            int dayEnd = calendarEnd.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[6] = yearEnd;
            yearMonthDay[7] = monthEnd;
            yearMonthDay[8] = dayEnd;
            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 开始日期范围设定
     * 已月为单位计量范围
     *
     * @param startMonth
     * @param endMonth
     * @return
     */
    public static Integer[] getMonthChange(int startMonth, int endMonth) {
        try {
            //时间集合
            Integer[] yearMonthDay = new Integer[9];
            Calendar calendar = Calendar.getInstance();
            //开始
            Calendar calendarStart = Calendar.getInstance();
            long startTimes = calendar.getTimeInMillis() - (startMonth * MONTH);
            calendarStart.setTimeInMillis(startTimes);
            // 获取年
            int yearStart = calendarStart.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthStart = calendarStart.get(Calendar.MONTH) + 1;
            // 获取日
            int dayStart = calendarStart.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[0] = yearStart;
            yearMonthDay[1] = monthStart;
            yearMonthDay[2] = dayStart;
            //当前
            // 获取年
            int year = calendar.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int month = calendar.get(Calendar.MONTH) + 1;
            // 获取日
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[3] = year;
            yearMonthDay[4] = month;
            yearMonthDay[5] = day;
            //结束
            Calendar calendarEnd = Calendar.getInstance();
            long endTimes = calendar.getTimeInMillis() + (endMonth * MONTH);
            calendarEnd.setTimeInMillis(endTimes);
            // 获取年
            int yearEnd = calendarEnd.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthEnd = calendarEnd.get(Calendar.MONTH) + 1;
            // 获取日
            int dayEnd = calendarEnd.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[6] = yearEnd;
            yearMonthDay[7] = monthEnd;
            yearMonthDay[8] = dayEnd;
            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 计算两个时间戳（秒）相差的时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String dateDiff(long startTime, long endTime) {
        Date date1 = new Date(startTime * 1000);
        Date date2 = new Date(endTime * 1000);
        SimpleDateFormat format_today = new SimpleDateFormat("yyyy-MM-dd");
        String str1 = format_today.format(date1);
        String str2 = format_today.format(date2);
        boolean isToday = false;
        if (str1.equals(str2)) {
            isToday = true;
        }
        // 按照传入的格式生成一个simpledateformate对象
        long nd = 24 * 60 * 60;// 一天的秒数
        long nh = 60 * 60;// 一小时的秒数
        long nm = 60;// 一分钟的秒数
        String differ_time = "";
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        // 获得两个时间的毫秒时间差异
        diff = endTime - startTime;
        day = diff / nd;// 计算差多少天
        hour = diff % nd / nh;// 计算差多少小时
        min = diff % nd % nh / nm;// 计算差多少分钟
        sec = diff % nd % nh % nm;// 计算差多少秒
        System.out.println("时间差：" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒");

        if (day == 0) {
            //时间相差不足一天
            if (isToday) {
                //是今天
                if (hour >= 3) {
                    Date date = new Date(endTime * 1000);
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                    differ_time = "今日 " + format.format(date);
                } else {
                    Date date = new Date(endTime * 1000);
                    differ_time = (hour < 10 ? "0" + hour : hour) + ":" + (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec);
                }
            } else {
                //肯定是明天
                Date date = new Date(endTime * 1000);
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                differ_time = "明日 " + format.format(date);
            }
        } else {
            //不是今天
            int cha = getGapCount(date1, date2);
            if (cha == 1) {
                //明天
                Date date = new Date(endTime * 1000);
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                differ_time = "明日 " + format.format(date);
            } else {
                //明天之后的时间
                Date date = new Date(endTime * 1000);
                SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
                differ_time = format.format(date);
            }
        }
//        //计算距离结束
//        differ_time = (day > 0 ? day + "天" : "")
//                + (hour > 0 ? (hour > 9 ? hour + ":" : "0" + hour + ":") : "00:")
//                + (min > 0 ? (min > 9 ? min + ":" : "0" + min + ":") : "00:")
//                + (sec > 0 ? (sec > 9 ? sec : "0" + sec) : "00");


        return differ_time;

    }

    public static int getGapCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * 结束日期范围设定，已开始时间为基准
     * 已月为单位计量范围
     *
     * @param startYear
     * @param startMonth
     * @param startDay
     * @param endMonth
     * @return
     */
    public static Integer[] getMonthChangeEnd(int startYear, int startMonth, int startDay, int endMonth) {
        try {
            //时间集合
            Integer[] yearMonthDay = new Integer[9];
            Calendar calendar = Calendar.getInstance();
            //开始
            yearMonthDay[0] = startYear;
            yearMonthDay[1] = startMonth;
            yearMonthDay[2] = startDay;
            //当前
            yearMonthDay[3] = startYear;
            yearMonthDay[4] = startMonth;
            yearMonthDay[5] = startDay;
            //结束
            Calendar calendarEnd = Calendar.getInstance();
            long endTimes = calendar.getTimeInMillis() + (endMonth * MONTH);
            calendarEnd.setTimeInMillis(endTimes);
            // 获取年
            int yearEnd = calendarEnd.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthEnd = calendarEnd.get(Calendar.MONTH) + 1;
            // 获取日
            int dayEnd = calendarEnd.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[6] = yearEnd;
            yearMonthDay[7] = monthEnd;
            yearMonthDay[8] = dayEnd;
            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //--------------------------------------------------- 活动 时间选择专用  start  -------------------------------------------


    /**
     * 以今天为标准，向前/后  推actionYear年，actionMonth月，actionDay日，得到那天日期
     * <p>
     * 注：往前推，则为负，往后推，则为正
     */
    public static Integer[] getYearMonthDay(int actionYear, int actionMonth, int actionDay) {
        try {
            //当前
            Calendar calendar = Calendar.getInstance();
            // 获取年
            int year = calendar.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int month = calendar.get(Calendar.MONTH) + 1;
            // 获取日
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            return getYearMonthDay(year, month, day, actionYear, actionMonth, actionDay);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 以compareYear,compareMonth,compareDay为标准，向前/后  推actionYear年，actionMonth月，actionDay日，得到那天日期
     * <p>
     * 注：往前推，则为负，往后推，则为正
     */
    public static Integer[] getYearMonthDay(int compareYear, int compareMonth, int compareDay, int actionYear, int actionMonth, int actionDay) {
        try {
            //当前
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, compareYear); //年
            calendar.set(Calendar.MONTH, compareMonth - 1); //月(第一个月为0 所以需减1)
            calendar.set(Calendar.DATE, compareDay); //日

            calendar.add(Calendar.YEAR, actionYear);
            calendar.add(Calendar.MONTH, actionMonth);
            calendar.add(Calendar.DATE, actionDay);
            // 获取年
            int year = calendar.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int month = calendar.get(Calendar.MONTH) + 1;
            // 获取日
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            Integer[] yearMonthDay = new Integer[3];
            yearMonthDay[0] = year;
            yearMonthDay[1] = month;
            yearMonthDay[2] = day;
            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 开始,结束日期范围设定( 以今日为标准 )
     * <p>
     * 0,1,2   为开始日期
     * 3,4,5   今日
     * 6,7,8   为结束日期
     *
     * @param startLimitYear,startLimitMonth 往前推startYear年startMonth月startDay天，需为负数
     * @param endLimitYear,endLimitMonth     往后推endYear年endMonth月endDay天，需为正数
     * @return
     */
    public static Integer[] getTimeLimit(int startLimitYear, int startLimitMonth, int startLimitDay, int endLimitYear, int endLimitMonth, int endLimitDay) {
        try {
            Integer[] yearMonthDay = new Integer[9];
            //时间集合
            Calendar calendar = Calendar.getInstance();

            //开始
            Calendar calendarStart = (Calendar) calendar.clone();
            calendarStart.add(Calendar.YEAR, startLimitYear);
            calendarStart.add(Calendar.MONTH, startLimitMonth);
            calendarStart.add(Calendar.DATE, startLimitDay);
            // 获取年
            int yearStart = calendarStart.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthStart = calendarStart.get(Calendar.MONTH) + 1;
            // 获取日
            int dayStart = calendarStart.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[0] = yearStart;
            yearMonthDay[1] = monthStart;
            yearMonthDay[2] = dayStart;

            //今日
            yearMonthDay[3] = calendar.get(Calendar.YEAR);
            yearMonthDay[4] = calendar.get(Calendar.MONTH) + 1;
            yearMonthDay[5] = calendar.get(Calendar.DAY_OF_MONTH);

            //结束
            Calendar calendarEnd = (Calendar) calendar.clone();
            calendarEnd.add(Calendar.YEAR, endLimitYear);   //往后推endYear年
            calendarEnd.add(Calendar.MONTH, endLimitMonth);
            calendarEnd.add(Calendar.DATE, endLimitDay);
            // 获取年
            int yearEnd = calendarEnd.get(Calendar.YEAR);
            // 获取月，这里需要需要月份的范围为0~11，因此获取月份的时候需要+1才是当前月份值
            int monthEnd = calendarEnd.get(Calendar.MONTH) + 1;
            // 获取日
            int dayEnd = calendarEnd.get(Calendar.DAY_OF_MONTH);
            yearMonthDay[6] = yearEnd;
            yearMonthDay[7] = monthEnd;
            yearMonthDay[8] = dayEnd;

            return yearMonthDay;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //判断某一日期，是否在某时间范围内( 前后包含 )
    public static boolean isInTimeRange(int compareYear, int compareMonth, int compareDay, int startYear, int startMonth, int startDay, int endYear, int endMonth, int endDay) {
        //时间集合
        Calendar calendar = Calendar.getInstance();
        //开始
        Calendar calendarStart = (Calendar) calendar.clone();
        calendarStart.set(Calendar.YEAR, startYear); //年
        calendarStart.set(Calendar.MONTH, startMonth - 1); //月(12个月，第一个月为0)
        calendarStart.set(Calendar.DATE, startDay); //日
        //结束
        Calendar calendarEnd = (Calendar) calendar.clone();
        calendarEnd.set(Calendar.YEAR, endYear); //年
        calendarEnd.set(Calendar.MONTH, endMonth - 1); //月
        calendarEnd.set(Calendar.DATE, endDay); //日
        //被比较日期
        Calendar calendarCompare = (Calendar) calendar.clone();
        calendarCompare.set(Calendar.YEAR, compareYear); //年
        calendarCompare.set(Calendar.MONTH, compareMonth - 1); //月
        calendarCompare.set(Calendar.DATE, compareDay); //日

        //Calendar 时间，通过 before 和 after比较，两者都不包含相等关系
        //如需判断相等关系，可通过非语句判断
        return !calendarCompare.after(calendarEnd) && !calendarCompare.before(calendarStart);
    }

    /**
     * compareDate与date比较
     * <p>
     * before 为true ,表示compareDate是否在date之前；false表示之后
     */
    public static boolean compareTime(int compareYear, int compareMonth, int compareDay, int year, int month, int day, boolean before) {
        //时间集合
        Calendar calendar = Calendar.getInstance();

        Calendar calendarStart = (Calendar) calendar.clone();
        calendarStart.set(Calendar.YEAR, year); //年
        calendarStart.set(Calendar.MONTH, month - 1); //月(12个月，第一个月为0)
        calendarStart.set(Calendar.DATE, day); //日

        Calendar calendarCompare = (Calendar) calendar.clone();
        calendarCompare.set(Calendar.YEAR, compareYear); //年
        calendarCompare.set(Calendar.MONTH, compareMonth - 1); //月
        calendarCompare.set(Calendar.DATE, compareDay); //日

        //Calendar 时间，通过 before 和 after比较，两者都不包含相等关系
        if (before) {
            return calendarCompare.before(calendarStart);
        } else {
            return calendarCompare.after(calendarStart);
        }
    }

    //输入年月日，得到标准日期
    public static String getDate(int year, int month, int day, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            //时间集合
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year); //年
            calendar.set(Calendar.MONTH, month - 1); //月(12个月，第一个月为0)
            calendar.set(Calendar.DATE, day); //日
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            return sdf.format(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }


    //--------------------------------------------------- 活动 时间选择专用  end  -------------------------------------------

    /**
     * 距离当前的时间间距
     *
     * @param rangeMilliseconds
     * @return
     */
    public static String endTimeRange(long rangeMilliseconds) {
        int rangeDay;
        long remainHour;
        int rangeHour;
        long remainMinute;
        int rangeMinute;
        long remainSeconds;
        int rangeSeconds;
        if (rangeMilliseconds > 0) {
            rangeDay = (int) (rangeMilliseconds / day);
            remainHour = (rangeMilliseconds % day);//剩余小时时间
            rangeHour = (int) (remainHour / hour);
            remainMinute = (remainHour % hour);
            rangeMinute = (int) (remainMinute / minute);
            remainSeconds = (remainMinute % minute);
            rangeSeconds = (int) (remainSeconds / 1000);
            if (rangeDay == 0 && rangeHour == 0 && rangeMinute == 0 && rangeSeconds > 0) {
                return rangeSeconds + "秒";
            } else {
                return (rangeDay > 0 ? rangeDay + "天" : "") + (rangeHour > 0 ? rangeHour + "小时" : "") + (rangeMinute > 0 ? rangeMinute + "分钟" : "") + "";
            }
        } else {
            return "";
        }
    }

    /**
     * 距离当前的时间间距
     *
     * @param rangeMilliseconds
     * @return
     */
    public static String[] endTimeRangeThree(long rangeMilliseconds) {
        int rangeDay;
        long remainHour;
        int rangeHour;
        long remainMinute;
        int rangeMinute;
        long remainSecond;
        int rangeSecond;
        if (rangeMilliseconds > 0) {
            rangeDay = (int) (rangeMilliseconds / day);
            remainHour = (rangeMilliseconds % day);//剩余小时时间
            rangeHour = (int) (remainHour / hour);
            remainMinute = (remainHour % hour);
            rangeMinute = (int) (remainMinute / minute);
            remainSecond = (remainMinute % minute);
            rangeSecond = (int) (remainSecond / 1000);
            return new String[]{rangeHour + "", rangeMinute > 9 ? rangeMinute + "" : "0" + rangeMinute, rangeSecond > 9 ? rangeSecond + "" : "0" + rangeSecond};
        } else {
            return null;
        }
    }

    /*
     * 返回当前的时间戳
     *
     * */
    public static long getCurrentMills() {
        long nowMilliseconds = System.currentTimeMillis() / 1000;
        return nowMilliseconds;
    }

    /*
     * 返回详细时间  时分秒
     *
     * */
    public static String showDetailTime(long oldMilliseconds) {
        return toFormatTime(oldMilliseconds * 1000, FORMAT_LONG);
    }

    /**
     * 将秒换算成天/时/分/秒
     *
     * @param mss
     * @return
     */
    public static Map<String, Long> getDHMS(long mss) {
        String DateTimes = null;
        Long days = mss / (60 * 60 * 24);
        Long hours = (mss % (60 * 60 * 24)) / (60 * 60);
        Long minutes = (mss % (60 * 60)) / 60;
        Long seconds = mss % 60;
        Map<String, Long> map = new HashMap<>();
        map.put("day", days);
        map.put("hour", hours);
        map.put("minute", minutes);
        map.put("second", seconds);
        return map;
//        if (days > 0) {
//            DateTimes = days + "天" + hours + "小时" + minutes + "分钟"
//                    + seconds + "秒";
//        } else if (hours > 0) {
//            DateTimes = hours + "小时" + minutes + "分钟"
//                    + seconds + "秒";
//        } else if (minutes > 0) {
//            DateTimes = minutes + "分钟"
//                    + seconds + "秒";
//        } else {
//            DateTimes = seconds + "秒";
//        }
    }


    /**
     * 距离当前的时间间距
     *
     * @param oldMilliseconds
     * @return
     */
    public static String shortPassedTimeRule(long oldMilliseconds) {
        long nowMilliseconds = System.currentTimeMillis() / 1000;
        long passed = (nowMilliseconds - oldMilliseconds);//转为秒
        if (passed > (DAY_SECONDS * 3)) {//大于3天
            return toFormatTime(oldMilliseconds * 1000, FORMAT_SHORT);
        } else if (passed > (DAY_SECONDS * 2)) {
            return "前天";
        } else if (passed > DAY_SECONDS) {
            return "昨天";
        } else if (passed > HOUR_SECONDS) {
            return passed / HOUR_SECONDS + "小时前";
        } else if (passed > MINUTE_SECONDS) {
            return passed / MINUTE_SECONDS + "分钟前";
        } else {
            return "刚刚";
        }
    }

    /**
     * 距离当前的时间间距
     *
     * @param oldMilliseconds
     * @return
     */
    public static String shortPassedTimeRuleForHome(long oldMilliseconds) {
        long nowMilliseconds = System.currentTimeMillis() / 1000;
        long passed = (nowMilliseconds - oldMilliseconds);//转为秒
        if (passed > DAY_SECONDS) {
            return (passed / DAY_SECONDS) + "天前";
        } else if (passed > HOUR_SECONDS) {
            return passed / HOUR_SECONDS + "小时前";
        } else if (passed > MINUTE_SECONDS) {
            return passed / MINUTE_SECONDS + "分钟前";
        } else {
            return "刚刚";
        }
    }


    /**
     * 获取两个日期的月数差
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static long getDifferMonth(Date fromDate, Date toDate) {
        Calendar fromDateCal = Calendar.getInstance();
        Calendar toDateCal = Calendar.getInstance();
        fromDateCal.setTime(fromDate);
        toDateCal.setTime(toDate);

        int fromYear = fromDateCal.get(Calendar.YEAR);
        int toYear = toDateCal.get((Calendar.YEAR));
        if (fromYear == toYear) {
            return Math.abs(fromDateCal.get(Calendar.MONTH) - toDateCal.get(Calendar.MONTH));
        } else {
            int fromMonth = 12 - (fromDateCal.get(Calendar.MONTH) + 1);
            int toMonth = toDateCal.get(Calendar.MONTH) + 1;
            return Math.abs(toYear - fromYear - 1) * 12 + fromMonth + toMonth;
        }
    }
}
